<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include('menu.php'); ?>

<script language = "JavaScript">

		//**** List Amphur (Start) ***//
		function ListSub(SelectValue)
		{
			document.formsearch.category_sub_id.length = 0;

			//*** Insert null Default Value ***//
			var myOption = new Option('','');

			
			<?php
			$strSQL = "SELECT * FROM category_sub ORDER BY category_sub_name ASC";
			$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
			$intRows = 0;
			while($objResult = mysql_fetch_array($objQuery))
			{
			$intRows++;
			?>
				x = <?php echo $intRows; ?>;
				mySubList = new Array();
				
				strGroup = <?php echo $objResult["category_id"];?>;
				strValue = "<?php echo $objResult["category_sub_id"];?>";
				strItem = "<?php echo $objResult["category_sub_name"];?>";
				mySubList[x,0] = strItem;
				mySubList[x,1] = strGroup;
				mySubList[x,2] = strValue;
							
				if (mySubList[x,1] == SelectValue){
					var myOption = new Option(mySubList[x,0], mySubList[x,2])  ;
					document.formsearch.category_sub_id.options[document.formsearch.category_sub_id.length]= myOption		;			
				}
			<?php
			}
			?>																	
		}
		//**** List Amphur (End) ***//

</script>
</head>
<body>
<?php
include("../connect/connect.php");
//ลบข้อมูล
if($_GET["action"]=='del') {

	$Query = "DELETE FROM product WHERE product_id = '".$_GET["id"]."'";
	$objQuery = mysql_query($Query);
	if(!$objQuery)
	{
		echo "Error Delete [".mysql_error()."]";
	}
	
	header('Location: product_manage.php');
}

//ค้นหาสินค้า
if($_POST["name_search"] != "" OR $_POST["unit_search"] != "" OR $_POST["category_search"] != "") {

	$Query = "SELECT * FROM product,unit,category,category_sub WHERE product.category_id = category.category_id AND product.category_sub_id = category_sub.category_sub_id AND product.unit_id=unit.unit_id ";
	
	if($_POST["name_search"] != "") {
		$Query .= "AND product.product_name LIKE '%".$_POST["name_search"]."%' ";
	}
	if($_POST["unit_search"] != "") {
		$Query .= "AND product.unit_id LIKE '%".$_POST["unit_search"]."%' ";
	}
	if($_POST["category_search"] != "") {
		$Query .= "AND product.category_id LIKE '%".$_POST["category_search"]."%' ";
	}
	if($_POST["category_sub_id"] != "") {
		$Query .= "AND product.category_sub_id LIKE '%".$_POST["category_sub_id"]."%' ";
	}
	
	$Query .= "ORDER BY product.product_name";
} else {
	$Query = "SELECT * FROM product,unit,category,category_sub WHERE product.category_id = category.category_id AND product.category_sub_id = category_sub.category_sub_id AND product.unit_id=unit.unit_id ORDER BY product.product_name";
}
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>จัดการสินค้า</h1><div class="buttons"><a onclick="JavaScript:window.location='product_add.php'" class="button"><span>เพิ่มสินค้า</span></a></div></div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td width="1" style="text-align: center;"><span class="center">ลำดับ</span></td>
            <td class="center">ชื่อสินค้า</td>
            <td width="15%" class="center">หน่วยนับ</td>
            <td width="15%" class="center">หมวดสินค้า</td>
            <td width="15%" class="center">หมวดสินค้าย่อย</td>
            <td width="15%" class="center">จัดการสินค้า</td>
          </tr>
        </thead>
        <tbody>
		<form action="<?php $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
          <tr class="filter">
            <td>&nbsp;</td>
            <td align="center"><input name="name_search" type="text" id="name_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><select name="unit_search" id="unit_search">
                <option value=""><-- Please Select --></option>
                <?php
			$Querycate = "SELECT * FROM unit ORDER BY unit_name ASC";
			$objQuerycate = mysql_query($Querycate);
			while($row = mysql_fetch_array($objQuerycate))
			{
			?>
                <option value="<?php echo $row["unit_id"];?>">
                <?php echo "  ".$row["unit_name"];?>
                </option>
                <?php
			}
			?>
              </select></td>
            <td align="center">
            <select name="category_search" id="category_search" onChange = "ListSub(this.value)" >
                <option value=""><-- Please Select --></option>
                <?php
			$Querycate = "SELECT * FROM category ORDER BY category_name ASC";
			$objQuerycate = mysql_query($Querycate);
			while($row = mysql_fetch_array($objQuerycate))
			{
			?>
                <option value="<?php echo $row["category_id"];?>">
                <?php echo "  ".$row["category_name"];?>
                </option>
                <?php
			}
			?>
              </select></td>
			<td align="center"><select name="category_sub_id" id="category_sub_id" style="min-width:140px;" ><option value=""><-- เลือกหมวดสินค้าก่อน --></option></select></td>
            <td align="center"><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a></td>
          </tr>
		</form>
<?php
	$num=0;
	while($row = mysql_fetch_array($objQuery))
		{
			$num++;
?>
          <tr>
            <td style="text-align: center;"><?php echo $num;?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["product_name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["unit_name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["category_name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["category_sub_name"];?></td>
 			<td class="center">[ <a href="product_edit.php?id=<?php echo $row["product_id"];?>">แก้ไข</a> ]  [ <a href="JavaScript:if(confirm('คุณต้องการยืนยันการลบข้อมูล ใช่หรือไม่?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?action=del&id=<?php echo $row["product_id"];?>';}">ลบ</a> ]</td>
          </tr>
<?php
		} 
?>
        </tbody>
      </table>
  </div>
</div>

</body>
<?php mysql_close(); ?>
</html>
